<!-- HTML header for doxygen 1.8.3.1-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.3.1"/>
<title>AngelScript: Context manager</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="test/javascript" src="touch.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.3.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('doc_addon_ctxmgr.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Context manager </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Path:</b> /sdk/add_on/contextmgr/</p>
<p>The <code>CContextMgr</code> is a class designed to aid the management of multiple simultaneous scripts executing in parallel. It supports both <a class="el" href="doc_adv_concurrent.html">concurrent script threads</a> and <a class="el" href="doc_adv_coroutine.html">co-routines</a>.</p>
<p>If the application doesn't need multiple contexts, i.e. all scripts that are executed always complete before the next script is executed, then this class is not necessary.</p>
<p>The context manager uses <a class="el" href="classas_i_script_engine.html#a32391ee83e58083b406ba068ab2ee049">asIScriptEngine::RequestContext</a> to take advantage of any context callbacks registered with the engine, e.g. for debugging or pooling.</p>
<p>Multiple context managers can be used, for example when you have a group of scripts controlling in-game objects, and another group of scripts controlling GUI elements, then each of these groups may be managed by different context managers.</p>
<p>Observe that the context manager class hasn't been designed for multi-threading, so you need to be careful if your application needs to execute scripts from multiple threads.</p>
<dl class="section see"><dt>See Also</dt><dd>The samples <a class="el" href="doc_samples_concurrent.html">Concurrent scripts</a> and <a class="el" href="doc_samples_corout.html">Co-routines</a> for uses</dd></dl>
<h1><a class="anchor" id="doc_addon_ctxmgr_1"></a>
Public C++ interface</h1>
<div class="fragment"><div class="line"><span class="keyword">class </span>CContextMgr</div>
<div class="line">{</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">  CContextMgr();</div>
<div class="line">  ~CContextMgr();</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Set the function that the manager will use to obtain the time in milliseconds.</span></div>
<div class="line">  <span class="keywordtype">void</span> SetGetTimeCallback(TIMEFUNC_t func);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Registers the following:</span></div>
<div class="line">  <span class="comment">//</span></div>
<div class="line">  <span class="comment">//  void sleep(uint milliseconds)</span></div>
<div class="line">  <span class="comment">//</span></div>
<div class="line">  <span class="comment">// The application must set the get time callback for this to work</span></div>
<div class="line">  <span class="keywordtype">void</span> RegisterThreadSupport(<a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Registers the following:</span></div>
<div class="line">  <span class="comment">//</span></div>
<div class="line">  <span class="comment">//  funcdef void coroutine(dictionary@)</span></div>
<div class="line">  <span class="comment">//  void createCoRoutine(coroutine @func, dictionary @args)</span></div>
<div class="line">  <span class="comment">//  void yield()</span></div>
<div class="line">  <span class="keywordtype">void</span> RegisterCoRoutineSupport(<a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Create a new context, prepare it with the function, then return </span></div>
<div class="line">  <span class="comment">// it so that the application can pass the argument values. The context</span></div>
<div class="line">  <span class="comment">// will be released by the manager after the execution has completed.</span></div>
<div class="line">  <span class="comment">// Set keepCtxAfterExecution to true if the application needs to retrieve</span></div>
<div class="line">  <span class="comment">// information from the context after it the script has finished. </span></div>
<div class="line">  <a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *AddContext(<a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine, <a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *func, <span class="keywordtype">bool</span> keepCtxAfterExecution = <span class="keyword">false</span>);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// If the context was kept after the execution, this method must be </span></div>
<div class="line">  <span class="comment">// called when the application is done with the context so it can be</span></div>
<div class="line">  <span class="comment">// returned to the pool for reuse.</span></div>
<div class="line">  <span class="keywordtype">void</span> DoneWithContext(<a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *ctx);</div>
<div class="line">  </div>
<div class="line">  <span class="comment">// Create a new context, prepare it with the function, then return</span></div>
<div class="line">  <span class="comment">// it so that the application can pass the argument values. The context</span></div>
<div class="line">  <span class="comment">// will be added as a co-routine in the same thread as the currCtx.</span></div>
<div class="line">  <a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *AddContextForCoRoutine(<a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *currCtx, <a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *func);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Execute each script that is not currently sleeping. The function returns after </span></div>
<div class="line">  <span class="comment">// each script has been executed once. The application should call this function</span></div>
<div class="line">  <span class="comment">// for each iteration of the message pump, or game loop, or whatever.</span></div>
<div class="line">  <span class="comment">// Returns the number of scripts still in execution.</span></div>
<div class="line">  <span class="keywordtype">int</span> ExecuteScripts();</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Put a script to sleep for a while</span></div>
<div class="line">  <span class="keywordtype">void</span> SetSleeping(<a class="code" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> *ctx, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> milliSeconds);</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Switch the execution to the next co-routine in the group.</span></div>
<div class="line">  <span class="comment">// Returns true if the switch was successful.</span></div>
<div class="line">  <span class="keywordtype">void</span> NextCoRoutine();</div>
<div class="line"></div>
<div class="line">  <span class="comment">// Abort all scripts</span></div>
<div class="line">  <span class="keywordtype">void</span> AbortAll();</div>
<div class="line">};</div>
</div><!-- fragment --><h1><a class="anchor" id="doc_addon_ctxmgr_2"></a>
Public script interface</h1>
<pre>
  funcdef void coroutine(dictionary@);
  void createCoRoutine(coroutine @, dictionary @);
  void yield();
</pre><p><b>funcdef void coroutine(dictionary@)</b><br/>
 <b>void createCoRoutine(coroutine @, dictionary @)</b></p>
<p>This function is used to create a co-routine. The co-routine will initiate in a yielded state, i.e. it will only begin execution once the control is given to it by the current thread.</p>
<p>Multiple co-routines can be created, and they will each take turn to execute in round-robin fashion.</p>
<p><b>void yield()</b></p>
<p>Yields control of the execution for the next co-routine in the queue.</p>
<p>When a co-routine receives control it will resume execution from the last call to yield, or the entry point if this is the first time the co-routine is allowed to execute. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Sun Dec 18 2016 12:35:27 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.3.1 </li>
  </ul>
</div>
</body>
</html>
